package com.cheng.cms.controller;

import com.cheng.cms.annotation.Log;
import com.cheng.cms.domain.Residents;
import com.cheng.cms.service.ResidentService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Api("用户管理API")
public class ResidentController {

    @Autowired
    private ResidentService residentService;


    @ApiOperation("跳转到用户信息页面")
    @GetMapping("/queryInfo/residentInfo")
    public String residentInfo(ModelMap modelMap){
        List<Residents> residents = residentService.selectAllResidents();
        modelMap.put("residentInfo",residents);
        return "queryInfo/residentInfo";
    }

    /**
     * 条件查询用户信息
     * @return
     */
    @Log(type = "查询所有用户信息")
    @ApiOperation("查询所有用户信息")
    @GetMapping("/queryInfo/residentInfo/condition")
    @ResponseBody
    public List<Residents> queryResidentByCondition(@ApiParam(name = "buildingNo",value = "用户楼号") String buildingNo,@ApiParam(name = "roomNo",value = "用户房号") String roomNo){
        Map<String, String> residentMap = new HashMap<>();
        if(!buildingNo.equals("全部")){
            residentMap.put("buildingNo",buildingNo);
        }
        if(!roomNo.equals("全部")){
            residentMap.put("roomNo",roomNo);
        }
        return residentService.selectResidentsByCondition(residentMap);
    }

    /**
     * 查询已有楼号、房号
     * @return
     */
    @Log(type = "查询已有楼号、房号")
    @ApiOperation("查询已有楼号、房号")
    @GetMapping("/queryInfo/residentInfo/buildingNoAndRoomNo")
    @ResponseBody
    public Map<String,List<String>> selectAllBuildingNo(){
        Map<String,List<String>> map = new HashMap<>();
        map.put("buildingNo",residentService.selectAllBuildingNo());
        map.put("roomNo",residentService.selectAllRoomNo());
        return map;
    }

    /**
     * 新增用户
     * @return
     */
    @ApiOperation("跳转到新增用户页面")
    @GetMapping("/sys/user/add")
    public String addUser(){
        return "/user/add";
    }

    /**
     * 新增保存用户
     * @return
     */
    @Log(type = "新增保存用户")
    @ApiOperation("新增保存用户")
    @PostMapping("/sys/user/add")
    @ResponseBody
    public String addUserSave(@ApiParam(name = "residents",value = "相当于一个resident实例，包含一个用户所有数据")Residents residents){
        System.out.println(residents);
        Map<String, String> residentMap = new HashMap<>();
        residentMap.put("buildingNo",residents.getBuildingNo());
        residentMap.put("roomNo",residents.getRoomNo());
        List<Residents> residentsList = residentService.selectResidentsByCondition(residentMap);
        System.out.println("用户数量"+residentsList.size());
        if(residentsList.size()>0){
            /**
             * 楼号房号已存在
             */
            return "该用户已存在";
        }else{
            int i = residentService.addUser(residents);
            System.out.println("i="+i);
            if(i==0){
                return "添加失败";
            }else {
                return "添加成功";
            }
        }

    }

    /**
     * 通过id删除用户
     * @param id
     * @return
     */
    @Log(type = "通过id删除用户")
    @ApiOperation("通过id删除用户")
    @GetMapping("/sys/user/delete")
    @ResponseBody
    public String deleteUserById(@ApiParam(name = "id",value = "用户id，前端请求携带")String id){
        int userID = Integer.parseInt(id);
        int i = residentService.deleteUserById(userID);
        if(i==0){
            return "fail";
        }else {
            return "success";
        }

    }

    /**
     * 跳转到用户更新页面
     * 通过用户id将旧的信息展示在页面
     * @param id
     * @return
     */
    @Log(type = "跳转到更新页面")
    @ApiOperation("跳转到更新页面")
    @ApiParam(name = "id",value = "用户id，前端请求携带到更新页面")
    @GetMapping("/sys/user/update")
    public String updateUser(@ApiParam(name = "id",value = "用户id") String id,ModelMap modelMap){
        Map<String, String> residentMap = new HashMap<>();
        residentMap.put("numberID",id);
        modelMap.put("updateUserInfo",residentService.selectResidentsByCondition(residentMap).get(0));
        return "/user/update";
    }

    @Log(type = "通过用户信息更新用户")
    @ApiOperation("通过用户信息更新用户")
    @PostMapping("/sys/user/update")
    @ResponseBody
    public String updateUserSave(@ApiParam(name = "residents",value = "单个用户的所有信息，相当于一个resident实例")Residents residents){
        int i = residentService.updateUser(residents);
        if(i==0){
            return "fail";
        }else {
            return "success";
        }
    }

    /**
     * 使用分页插件返回分页后的用户数据
     */

    @GetMapping("/sys/page/{pages}")
    @ResponseBody
    public PageInfo<Residents> getResidentByPage(@PathVariable int pages){
        //        设置分页规则
        PageHelper.startPage(pages,2);
        //        返回所有分页信息参数为查询所有记录的信息
        PageInfo<Residents> pageInfo = new PageInfo<>(residentService.selectAllResidents());
        //System.out.println("查询信息=============>"+pageInfo.getList().get(0));
        System.out.println(pageInfo.toString());
        return pageInfo;
    }
}
